package cn.com.git.workflow.dao.impl;

import cn.com.git.common.base.BaseDaoImpl;
import cn.com.git.workflow.dao.WorkFlowOperateDAO;
import cn.com.git.workflow.dao.mapper.WorkFlowOperateMapper;
import cn.com.git.workflow.dao.model.WorkFlowOperate;
import cn.com.git.workflow.dao.model.WorkFlowOperateExample;
import cn.com.git.workflow.service.query.WorkFlowOperateListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
/**
  * WorkFlowOperateDAO接口
  * @author zhangzhenfu
  * @Description
  * @date 2020/4/14
*/
@Repository
public class WorkFlowOperateDAOImpl extends BaseDaoImpl<WorkFlowOperateMapper,WorkFlowOperate,WorkFlowOperateExample,WorkFlowOperateListQuery,Integer> implements WorkFlowOperateDAO {

    @Autowired
    private WorkFlowOperateMapper mapper;

    @Override
    protected WorkFlowOperateMapper getMapper() {
        return mapper;
    }

    @Override
    public WorkFlowOperateExample analysisLimit(WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = new WorkFlowOperateExample();
        WorkFlowOperateExample.Criteria c =  example.createCriteria();
        if(query.getId()!=null){
            c.andIdEqualTo(query.getId());
        }
        if(query.getCurrentNode()!=null){
            c.andCurrentNodeEqualTo(query.getCurrentNode());
        }
        if(query.getOperatorId()!=null){
            c.andOperatorIdEqualTo(query.getOperatorId());
        }
        if(query.getWorkFlowId()!=null){
            c.andWorkFlowIdEqualTo(query.getWorkFlowId());
        }
        if(query.getState()!=null){
            c.andStateEqualTo(query.getState());
        }
        return example;
    }

    @Override
    public WorkFlowOperate selectById(Integer id) {
        return super.selectByPrimaryKey(id);
    }

    @Override
    public WorkFlowOperate selectOne(WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = analysisLimit(query);
        List<WorkFlowOperate> list =   Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
            if(list.size()>0){
                return list.get(0);
            }
        return null;
    }

    @Override
    public List<WorkFlowOperate> selectList(WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = analysisLimit(query);
        return Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
    }

    @Override
    public int countLimit(WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = analysisLimit(query);
        return countByExample(example);
    }

    @Override
    public Integer insertOne(WorkFlowOperate record) {
        super.insertSelective(record);
        return record.getId();
    }

    @Override
    public List<WorkFlowOperate> insertList(List<WorkFlowOperate> list) {
        Optional.ofNullable(list).orElse(Lists.newArrayList()).forEach(s->{
            super.insertSelective(s);
        });
        return list;
    }

    @Override
    public Integer updateOne(WorkFlowOperate record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateLimit(WorkFlowOperate record, WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = analysisLimit(query);
        return super.updateByExampleSelective(record,example);
    }

    @Override
    public Integer deleteOne(Integer id) {
        return super.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLimit(WorkFlowOperateListQuery query) {
        WorkFlowOperateExample example = analysisLimit(query);
        return super.deleteByExample(example);
    }

}